package com.kabu.aqs;

import org.testng.annotations.Test;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author jiawuwei
 * @Copyright:
 * @date 2020/7/31,15:59
 */
public class TestAQS {
    public static int  count = 0;
    public static AtomicInteger count1=new AtomicInteger(0);
    static AQSLock lock = new AQSLock();
    @Test(threadPoolSize = 10, invocationCount = 10, timeOut = 0)
    public static void count1(){
        while (true) {
            count1.decrementAndGet();
            //加入线程T1执行到if判断处，此时他的值为
            if (count1.get() == 100) {
                break;
            }

        }
        System.out.println(count1.get());
    }

    @Test(threadPoolSize = 10, invocationCount = 10, timeOut = 0)
    public static void count(){
        lock.lock();
        synchronized (TestAQS.class){
            while (true) {
                //如果t1执行完count++后为100，然后t2夺取了执行时间片，此时count会一直加
                //count++;
                if (count == 100) {
                    break;
                }
                count++;
                System.out.println(1);
                if(count%100==0){
                    //System.out.println(Thread.currentThread().getName()+" before-:"+count);
                }
            }
            System.out.println(Thread.currentThread().getName()+" before:"+count);
        }
        lock.unlock();
        System.out.println(Thread.currentThread().getName()+" after:"+count);
    }


}
